package com.konsung.ksdc.manager.shop;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.ProductInfoBean;
import com.konsung.ksdc.bean.netbean.AddrInfoNetBean;
import com.konsung.ksdc.bean.netbean.CartDetailNetBean;
import com.konsung.ksdc.bean.netbean.ExpressDetailNetBean;
import com.konsung.ksdc.bean.netbean.OrderItemNetBean;
import com.konsung.ksdc.bean.netbean.OrderProductNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.common.utils.ToastUtils;
import com.konsung.ksdc.common.widget.ConfirmPopupWindow;
import com.konsung.ksdc.common.widget.ExpandListView;
import com.konsung.ksdc.common.widget.WaitingDialog;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 订单详情页面
 * Created by Administrator on 2016/5/17 0017.
 */
public class OrderCommitOkActivity extends BaseActivity implements View.OnClickListener {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tabTvTitle;
    @Bind(R.id.tv_save)
    TextView tvSave;
    @Bind(R.id.rl_title_container)
    RelativeLayout rlTitleContainer;
    @Bind(R.id.lv_order_item_list)
    ExpandListView lvOrderItemList;
    @Bind(R.id.tv_receiver)
    TextView tvReceiver;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.tv_addr_detail)
    TextView tvAddrDetail;
    @Bind(R.id.btn_cancel_order)
    Button btnCancelOrder;
    @Bind(R.id.iv_submit_order)
    ImageView ivSubmitOrder;
    @Bind(R.id.tv_submit_text)
    TextView tvSubmitText;
    @Bind(R.id.iv_connect_one)
    ImageView ivConnectOne;
    @Bind(R.id.iv_send_order)
    ImageView ivSendOrder;
    @Bind(R.id.tv_send_text)
    TextView tvSendText;
    @Bind(R.id.iv_connect_two)
    ImageView ivConnectTwo;
    @Bind(R.id.iv_sign_in)
    ImageView ivSignIn;
    @Bind(R.id.tv_sign_text)
    TextView tvSignText;
    @Bind(R.id.iv_connect_three)
    ImageView ivConnectThree;
    @Bind(R.id.iv_order_complete)
    ImageView ivOrderComplete;
    @Bind(R.id.tv_complete_text)
    TextView tvCompleteText;
    @Bind(R.id.rl_order_cancel)
    RelativeLayout rlOrderCancel;
    @Bind(R.id.tv_order_number)
    TextView tvOrderNumber;
    @Bind(R.id.tv_count_text)
    TextView tvCountText;
    @Bind(R.id.tv_pay_text)
    TextView tvPayText;
    @Bind(R.id.ll_submit)
    LinearLayout llSubmit;
    @Bind(R.id.tv_order_time)
    TextView tvOrderTime;
    @Bind(R.id.ll_send)
    LinearLayout llSend;
    @Bind(R.id.tv_send_time)
    TextView tvSendTime;
    @Bind(R.id.tv_express)
    TextView tvExpress;
    @Bind(R.id.tv_express_number)
    TextView tvExpressNumber;
    @Bind(R.id.ll_pay)
    LinearLayout llPay;
    @Bind(R.id.tv_complete_time)
    TextView tvCompleteTime;
    @Bind(R.id.btn_evaluate)
    Button btnEvaluate;
    @Bind(R.id.btn_see_evaluate)
    Button btnSeeEvaluate;
    @Bind(R.id.rl_order_closed)
    RelativeLayout rlOrderClosed;
    @Bind(R.id.tv_close_time)
    TextView tvCloseTime;
    @Bind(R.id.btn_delete_order)
    Button btnDeleteOrder;

    private ArrayList<CartDetailNetBean> datas = new ArrayList<>();
    private AddrInfoNetBean addrBean;
    private String orderStatus = "0";
    private OrderItemNetBean orderItemNetBean;
    private List<OrderProductNetBean> productNetBeanList;
    private ExpressDetailNetBean expressDetailNetBeen;
    private OrderBuyListAdapter orderBuyListAdapter;
    public static final String ORDER_ID = "orderId";
    public static final String BUY_LIST_DATA = "buy_list_data";
    public static final String ORDER_TYPE = "order_type";
    private Intent intent;
    private String orderId;
    private ConfirmPopupWindow popupWindow; // 删除订单确认框
    private WaitingDialog waitingDialog;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            initView();
        }
    };

    @Override
    protected String activityDesc() {
        return getString(R.string.page_commit_order);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_commit_ok);
        ButterKnife.bind(this);

        initEvent();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Constants.productStatusChanged) {
            RequestUtils.findOrderByOrderId(orderId, new RequestCallback() {
                @Override
                public void onError(Call call, Exception e) {

                }

                @Override
                public void onResponse(ResponseBean responseBean) {
                    String resultCode = responseBean.getResultCode();
                    if (resultCode.equals(Constants.REQUEST_OK)) {
                        orderItemNetBean = responseBean.getEntity(OrderItemNetBean.class);
                        productNetBeanList = orderItemNetBean.getConfs();
                        orderBuyListAdapter = new OrderBuyListAdapter(productNetBeanList,
                                orderStatus, true);
                        lvOrderItemList.setAdapter(orderBuyListAdapter);
                    }
                }
            });
        }
        Constants.productStatusChanged = false;
    }

    /**
     * 初始化事件
     */
    private void initEvent() {
        rlBack.setOnClickListener(this);
        btnCancelOrder.setOnClickListener(this);
        btnEvaluate.setOnClickListener(this);
        btnSeeEvaluate.setOnClickListener(this);
        btnDeleteOrder.setOnClickListener(this);
        lvOrderItemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ProductInfoBean bean = new ProductInfoBean(orderItemNetBean.getConfs()
                        .get(position).getProductId(), "", "", "", "");
                Intent intent = new Intent(OrderCommitOkActivity.this, ProductDetailActivity.class);
                String productIconUrl = bean.getProductIconUrl();

                intent.putExtra(ProductDetailActivity.PRODUCT_INFO_DATA, bean);
                intent.putExtra(ProductDetailActivity.PRODUCT_ICON_URL, productIconUrl);
                startActivity(intent);
            }
        });
    }

    /**
     * 初始化订单详情界面
     */
    private void initView() {
        orderStatus = orderItemNetBean.getStatus();
        addrBean = orderItemNetBean.getAddress();
        productNetBeanList = orderItemNetBean.getConfs();
        expressDetailNetBeen = orderItemNetBean.getAsdd();
        tvReceiver.setText(addrBean.getUserName());
        tvPhone.setText(addrBean.getPhone());
        tvAddrDetail.setText(addrBean.getDetailAddress());
        refreshOrderStatus();
        orderBuyListAdapter = new OrderBuyListAdapter(productNetBeanList, orderStatus, true);
        lvOrderItemList.setAdapter(orderBuyListAdapter);
        tvOrderNumber.setText(orderItemNetBean.getOrderId());
        tvCountText.setText(StringUtils.appendCount(productNetBeanList.size()));
        tvPayText.setText(orderItemNetBean.getTotalPrice());
        tvOrderTime.setText(orderItemNetBean.getCreateTime());
        if (null != expressDetailNetBeen) {
            tvSendTime.setText(expressDetailNetBeen.getSendTime());
            tvExpress.setText(expressDetailNetBeen.getExpressCompany());
            tvExpressNumber.setText(expressDetailNetBeen.getExpressCode());
        }
        tvCompleteTime.setText(orderItemNetBean.getReceiptTime());
    }

    /**
     * 初始化订单详情数据
     */
    private void initData() {
        tabTvTitle.setText(R.string.title_order_detail);
        lvOrderItemList.setFocusable(false);
        datas = getIntent().getParcelableArrayListExtra(OrderConfirmActivity
                .DATA_BUY_LIST);
        String orderType = getIntent().getStringExtra(ORDER_TYPE);
        switch (orderType) {
            case "1":
                orderId = getIntent().getStringExtra(ORDER_ID);
                RequestUtils.findOrderByOrderId(orderId, new RequestCallback() {
                    @Override
                    public void onError(Call call, Exception e) {

                    }

                    @Override
                    public void onResponse(ResponseBean responseBean) {
                        String resultCode = responseBean.getResultCode();
                        if (resultCode.equals(Constants.REQUEST_OK)) {
                            orderItemNetBean = responseBean.getEntity(OrderItemNetBean.class);
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
                break;
            case "2":
                orderItemNetBean = getIntent().getParcelableExtra(MyOrderActivity.ORDER_DATA);
                orderId = orderItemNetBean.getOrderId();
                initView();
                break;
            default:
                break;
        }
    }

    /**
     * 刷新订单状态
     */
    private void refreshOrderStatus() {
        switch (orderStatus) {
            case Constants.ORDER_TYPE_NOT_SEND:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_sel);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_nor);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_nor);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_nor);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_nor);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                rlOrderCancel.setVisibility(View.VISIBLE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.GONE);
                llPay.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.VISIBLE);
                btnSeeEvaluate.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                break;
            case Constants.ORDER_TYPE_NOT_RECEIVED:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_sel);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_sel);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_nor);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_nor);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_nor);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                rlOrderCancel.setVisibility(View.GONE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.VISIBLE);
                llPay.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnSeeEvaluate.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                break;
            case Constants.ORDER_TYPE_NOT_COMMENT:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_sel);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_sel);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_sel);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_nor);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_nor);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                rlOrderCancel.setVisibility(View.VISIBLE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.VISIBLE);
                llPay.setVisibility(View.VISIBLE);
                btnEvaluate.setVisibility(View.VISIBLE);
                btnCancelOrder.setVisibility(View.GONE);
                btnSeeEvaluate.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                break;
            case Constants.ORDER_TYPE_SUCCESS:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_sel);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_sel);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_sel);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_sel);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_sel);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                rlOrderCancel.setVisibility(View.VISIBLE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.VISIBLE);
                llPay.setVisibility(View.VISIBLE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnSeeEvaluate.setVisibility(View.VISIBLE);
                break;
            case Constants.ORDER_TYPE_CLOSED:
                rlOrderClosed.setVisibility(View.VISIBLE);
                rlOrderCancel.setVisibility(View.VISIBLE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.GONE);
                llPay.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.VISIBLE);
                btnSeeEvaluate.setVisibility(View.GONE);
                tvCloseTime.setText(orderItemNetBean.getCloseTime());
                break;
            case Constants.ORDER_TYPE_CHECK:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_sel);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_sel);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_sel);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_sel));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_nor);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_nor);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_nor);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                rlOrderCancel.setVisibility(View.GONE);
                llSubmit.setVisibility(View.VISIBLE);
                llSend.setVisibility(View.VISIBLE);
                llPay.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnSeeEvaluate.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                break;
            default:
                rlOrderClosed.setVisibility(View.GONE);
                ivSubmitOrder.setImageResource(R.drawable.ic_submit_order_nor);
                tvSubmitText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectOne.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSendOrder.setImageResource(R.drawable.ic_send_order_nor);
                tvSendText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectTwo.setImageResource(R.drawable.ic_pointgroup_nor);
                ivSignIn.setImageResource(R.drawable.ic_sign_in_nor);
                tvSignText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                ivConnectThree.setImageResource(R.drawable.ic_pointgroup_nor);
                ivOrderComplete.setImageResource(R.drawable.ic_order_complete_nor);
                tvCompleteText.setTextColor(ContextCompat.getColor(this, R.color.text_nor));
                rlOrderCancel.setVisibility(View.GONE);
                llSubmit.setVisibility(View.GONE);
                llSend.setVisibility(View.GONE);
                llPay.setVisibility(View.GONE);
                btnEvaluate.setVisibility(View.GONE);
                btnCancelOrder.setVisibility(View.GONE);
                btnSeeEvaluate.setVisibility(View.GONE);
                btnDeleteOrder.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_cancel_order:
                intent = new Intent(this, OrderCancelActivity.class);
                intent.putExtra(ORDER_ID, orderItemNetBean.getOrderId());
                startActivity(intent);
                finish();
                break;
            case R.id.btn_evaluate:
                intent = new Intent(this, OrderCommentActivity.class);
                intent.putParcelableArrayListExtra(BUY_LIST_DATA, (ArrayList<? extends Parcelable>)
                        productNetBeanList);
                String orderId = orderItemNetBean.getOrderId();
                intent.putExtra(ORDER_ID, orderId);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_see_evaluate:
                intent = new Intent(this, OrderEvaluateDetailActivity.class);
                intent.putParcelableArrayListExtra(BUY_LIST_DATA, (ArrayList<? extends Parcelable>)
                        productNetBeanList);
                intent.putExtra(MyOrderActivity.ORDER_DATA, orderItemNetBean);
                startActivity(intent);
                break;
            case R.id.btn_delete_order:
                deleteOrder();
                break;
            default:
                break;
        }
    }

    /**
     * 删除订单
     */
    private void deleteOrder() {
        popupWindow = new ConfirmPopupWindow(OrderCommitOkActivity.this, new View
                .OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                waitingDialog = new WaitingDialog(OrderCommitOkActivity.this, "");
                waitingDialog.setText(getString(R.string.deleting));
                waitingDialog.show();
                RequestUtils.deleteOrder(orderItemNetBean.getOrderId(), new
                        RequestCallback() {

                            @Override
                            public void onError(Call call, Exception e) {
                                waitingDialog.dismiss();
                                ToastUtils.toastContent(OrderCommitOkActivity.this,
                                        getString(R.string.delete_order_fail));
                            }

                            @Override
                            public void onResponse(ResponseBean responseBean) {
                                waitingDialog.dismiss();
                                if (Constants.REQUEST_OK.equals(responseBean
                                        .getResultCode())) {
                                    ToastUtils.toastContent(OrderCommitOkActivity.this,
                                            getString(R.string.tip_delete_ok));
                                    finish();
                                } else {
                                    ToastUtils.toastContent(OrderCommitOkActivity.this,
                                            getString(R.string.delete_order_fail));
                                }
                            }
                        });
            }
        },

                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
        popupWindow.setTips(getString(R.string.delete_order_confirm));
        popupWindow.setTitle(getString(R.string.delete_order));
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }
}
